package com.fuli.app.controller;

import com.fuli.app.common.ActionResult;
import com.fuli.app.common.ErrorInfo;
import com.fuli.app.common.Pagination;
import com.fuli.app.dto.CurriculumDTO;
import com.fuli.app.dto.query.CurriculumQueryDTO;
import com.fuli.app.service.CurriculumService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;

@Api(tags = "课程")
@RestController
@RequestMapping("/curriculum")
public class CurriculumController {
    @Autowired
    private CurriculumService curriculumService;
//    public static final long MAX_SIZE = 1024 * 1024 * 1024;
//    public static final Set<String> IMAGE_SUFFIX_SET = new HashSet<>(Arrays.asList(
//            "AVI", "avi", "mp4", "MP4", "MPEG4", "FLV", "flv"));

    @ApiOperation(
            value = "新增课程",
            notes = "<b>必填属性：</b><br>" +
                    "1.file 视频本地路径<br/>" +
                    "2.name 课程名<br/>" +
                    "3.notice 公告<br/>" +
                    "4.intro 简介<br/>" +
                    "5.categoryId 课程分类id<i><b>如果没有分类则categoryId = root</b></i><br/>"
    )
    @PostMapping(value = "/add")
    public ActionResult<String> add(@ApiParam("视频本地路径") @RequestParam String file,
                                    @ApiParam("课程名") @RequestParam String name,
                                    @ApiParam("公告") @RequestParam String notice,
                                    @ApiParam("视频分类id") @RequestParam String categoryId,
                                    @ApiParam("简介") @RequestParam String intro) {
//        if (file.getSize() > MAX_SIZE)
//            return new ActionResult<>(0, "视频大小超过限制");
//        if (file.isEmpty())
//            return new ActionResult<>(0, "文件不存在");
//        int lastIndex = file.getOriginalFilename().lastIndexOf('.');
//        if (lastIndex == -1)
//            return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
//        String suffix = file.getOriginalFilename().substring(lastIndex + 1);
//        if (StringUtils.isEmpty(suffix))
//            return new ActionResult<>(0, "文件名称错误，缺失文件后缀");
//        if (!IMAGE_SUFFIX_SET.contains(suffix))
//            return new ActionResult<>(0, "文件格式不支持，仅支持avi,mp4,flv格式");
        if (StringUtils.isEmpty(name))
            return new ActionResult<>(0, "课程名不能为空");
        if (StringUtils.isEmpty(categoryId))
            return new ActionResult<>(0, "视频分类id不能为空");
        if (notice == null)
            notice = " ";
        if (intro == null)
            intro = " ";
        return curriculumService.add(file, name, notice, categoryId, intro);
    }

    @ApiOperation(value = "删除课程")
    @DeleteMapping(value = "/delete/{id}")
    public ActionResult<String> delete(@ApiParam("课程id") @PathVariable String id) {
        return curriculumService.delete(id);
    }

    @ApiOperation(value = "批量删除课程")
    @DeleteMapping(value = "/batchDelete")
    public ActionResult<String> batchDelete(@ApiParam("课程id数组") @RequestParam List<String> ids) {
        if (CollectionUtils.isEmpty(ids))
            return new ActionResult<>(0, "参数不能为空");
        if (ids.size() > 10)
            return new ActionResult<>(0, "批量删除数量不能超过10个");
        return curriculumService.batchDelete(ids);
    }

    @ApiOperation(
            value = "修改课程",
            notes = "<b>必填属性：</b><br>" +
                    "1.id 主键<br/>" +
                    "2.课程名 名称<br/>" +
                    "3.categoryId 课程分类id<i><b>如果没有分类则categoryId = root</b></i><br/>"
    )
    @PutMapping(value = "/update")
    public ActionResult<String> update(@ApiParam("课程DTO") @RequestBody CurriculumDTO dto) {
        List<ErrorInfo> errors = new ArrayList<>();
        if (StringUtils.isEmpty(dto.getId()))
            errors.add(new ErrorInfo(0, "id为空"));
        if (StringUtils.isEmpty(dto.getName()))
            errors.add(new ErrorInfo(0, "课程名为空"));
        if (StringUtils.isEmpty(dto.getCategoryId()))
            errors.add(new ErrorInfo(0, "课程分类id为空"));
        if (!errors.isEmpty())
            return new ActionResult<>(errors);
        return curriculumService.update(dto);
    }

    @ApiOperation(value = "获取课程")
    @GetMapping(value = "/get/{id}")
    public ActionResult<CurriculumDTO> get(@ApiParam("课程id") @PathVariable String id) {
        return curriculumService.get(id);
    }

    @ApiOperation("升序")
    @PutMapping("/up/{id}")
    public ActionResult<String> up(@ApiParam("id") @PathVariable String id) {
        return curriculumService.up(id);
    }

    @ApiOperation("降序")
    @PutMapping("/down/{id}")
    public ActionResult<String> down(@ApiParam("id") @PathVariable String id) {
        return curriculumService.down(id);
    }

    @ApiOperation(value = "刷新指定课程分类下的课程排序")
    @PutMapping(value = "/refresh/{categoryId}")
    public ActionResult<String> refresh(@ApiParam("课程分类id") @PathVariable String categoryId) {
        return curriculumService.refresh(categoryId);
    }

    @ApiOperation("查询指定条件的课程")
    @GetMapping("/pageQuery")
    public ActionResult<Pagination<CurriculumDTO>> pageQuery(@ApiParam("课程查询DTO") @ModelAttribute CurriculumQueryDTO queryDTO) {
        //验证页码
        if (queryDTO.getPageNo() == null || queryDTO.getPageNo() <= 0)
            queryDTO.setPageNo(1);
        if (queryDTO.getPageSize() == null || queryDTO.getPageSize() <= 0)
            queryDTO.setPageSize(10);
        return curriculumService.listByQueryDTO(queryDTO);
    }


}
